﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace AUV5.Web.Pages
{
    public partial class RequestList : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindCategory();

                ViewState["sqlwhere"] = string.Format("State={0} and IsDel={1}",(int)Common.Enum.auvState.Enabled, (int)Common.Enum.auvDel.NotDel);
                if (Request["command"] != null)
                {
                    if (Request["command"] == "manage"&&base.IsVerified)
                    {
                        publishPanel.Visible = true;
                        searchPanel.Visible = false;    
                        ViewState["sqlwhere"] += string.Format(" and UserID='{0}'", base.VerifiedUserID);
                    }
                    else
                    {
                        Response.Redirect("RequestList.aspx");
                    }
                }
                else
                {
                    ViewState["sqlwhere"] += string.Format(" and StatusID={0}", (int)Common.Enum.RequestState.Enable);
                }

                bindRequest();
            }
        }

        //绑定类别
        private void bindCategory()
        {
            ddlCategory.DataSource = BLL.ServiceCategoryBLL.Instance.GetActiveList();
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataBind();
        }


        //绑定需求列表
        private void bindRequest()
        {
            if (Request["param"] != null)
            {
                ViewState["sqlwhere"] += string.Format(" and Title like '%{0}%'", Request["param"]);
                txtParam.Text = Request["param"];
            }
            int recordcount = 0;

            repRequest.DataSource = BLL.RequestInfoBLL.Instance.GetPagedList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, ViewState["sqlwhere"].ToString(), ref recordcount);
            repRequest.DataBind();

            AspNetPager1.RecordCount = recordcount;
        }


        //换页
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            bindRequest();
        }

        //发布
        protected void ibtnAddRequest_Click(object sender, ImageClickEventArgs e)
        {
            if (base.IsVerified)
            {
                if (txtRequestTitle.Text.Trim() != "")
                {
                    Model.RequestInfo request = new Model.RequestInfo();
                    request.RequestID = Guid.NewGuid().ToString();
                    request.UserID = base.VerifiedUserID;
                    request.Title = txtRequestTitle.Text.Trim();
                    request.Description = "";
                    request.CategoryName = ddlCategory.SelectedItem.Value;

                    request.Price = decimal.Parse(ddlPrice.SelectedItem.Value);
                    request.StatusID = (int)Common.Enum.RequestState.UnTreated;

                    request.RequestFrom = Request.UserHostAddress;

                    BLL.RequestInfoBLL.Instance.Add(request);
                    Response.Redirect(Request.Url.ToString());
                }
            }
            else
            {
                Response.Redirect(URL_LOGIN);
            }
        }

        //搜索
        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("RequestList.aspx?param=" + txtParam.Text.Trim());
        }

        //
        protected void repRequest_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HtmlGenericControl contactMe = (HtmlGenericControl)e.Item.FindControl("contactMe");
            LinkButton requestDel = (LinkButton)e.Item.FindControl("requestDel");
            string userid=((HiddenField)e.Item.FindControl("hfUserID")).Value;

            if (!base.IsVerified)
            {
                contactMe.Visible = false;
                requestDel.Visible = false; 
            }
            else
            {
                if (userid == base.VerifiedUserID)
                {
                    contactMe.Visible = false;
                    requestDel.Visible = true;
                }
                else
                {
                    contactMe.Visible = true;
                    requestDel.Visible = false;
                }
            }
        }

        protected void repRequest_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //删除
            if (e.CommandName == "Delete")
            {
                string requestID = e.CommandArgument.ToString();

                Model.RequestInfo request=BLL.RequestInfoBLL.Instance.GetDetail(requestID);

                if(request!=null)
                {
                    request.IsDel = (int)Common.Enum.auvDel.Del;
                    request.UpdateDate = DateTime.Now;
                    BLL.RequestInfoBLL.Instance.Update(request);
                }
                Response.Redirect(Request.Url.ToString());
            }
        }
    }
}